<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\assignment;

use lflow\assignment\ipml\Proposer;
use lflow\workflow\Execution;
use lflow\model\TaskModel;
use lflow\assignment\ipml\FormsUser;
use lflow\assignment\ipml\NamedUser;
use lflow\assignment\ipml\NamedRole;
use lflow\assignment\ipml\DirectManager;
use think\Exception;

/**
 * 分配参与者处理类
 *
 * @author Mr.April
 * @since  1.0
 */
class AssignmentTask extends Assignment
{
    /**
     * assign
     *
     * @param \lflow\model\TaskModel    $model
     * @param \lflow\workflow\Execution $execution
     *
     * @return string
     */
    public function assign(TaskModel $model, Execution $execution): string
    {
        $scope         = (int)$model->getScope();
        $assign        = self::ASSIGN;
        $assignHandler = self::ASSIGNMENT_HANDLER;
        if (isset($model->$assignHandler) && !empty($model->$assignHandler)) {
            //如果存在自定义类使用自定义类处理
            return (new $model->$assignHandler($model, $execution));
        }
        switch ($scope) {
            case self::NAMED_USER:
                $examples = new NamedUser();
                break;
            case self::NAMED_ROLE:
                $examples = new NamedRole();
                break;
            case self::FORMS_USER:
                $examples = new FormsUser();
                break;
            case self::NAMED_BOSS:
                $examples = new DirectManager();
                break;
            case self::PROPOSER:
                $examples = new Proposer();
                break;
            default:
                throw new Exception('未定义任务分配类型');
        }
        return $examples->assign($model, $execution);
    }
}
